"""
题目36：求100之内的素数。
"""


def timu36(fanwei):
    sushu = [2]
    for i in range(3, fanwei, 2):
        linshi = []
        # 左闭右开，所以要加1，才能包含平方根本身
        for j in range(2, int(i**0.5)+1):
            if (i % j) == 0:
                linshi.append(j)
        if not linshi:
            sushu.append(i)
    print(sushu)


# timu36(100)


def timu36_ai(fanwei):
    sushu = [2]  # 先添加素数2
    for i in range(3, fanwei, 2):  # 跳过偶数，从3开始
        linshi = []  # 每次循环重置临时列表
        for j in range(2, int(i**0.5) + 1):  # 检查2到√i
            if i % j == 0:
                linshi.append(j)
        if not linshi:  # 若无因数则为素数
            sushu.append(i)
    print(sushu)


# timu36_ai(100)


def timu36_ai1(fanwei):
    sushu = [2]

    for i in range(3, fanwei, 2):
        is_su = True
        for j in range(2, int(i**0.5) + 1):
            # print("本次循环 i 是{}， j是{}".format(i, j))
            yushu = i % j
            if yushu == 0:
                # 因为9循环中，is_su被置为了false，所以11以及后续的值都在后续判断中，没有添加到队列；
                # 所以应该把 is_su = True 放到外循环内去
                is_su = False
                break
        if is_su:  # 若无因数则为素数
            sushu.append(i)
    print(sushu)


# timu36_ai1(100)

"""
题目37：对10个数进行排序。
"""


def timu37(shulist):
    print(sorted(shulist))
    print(shulist)
    print(shulist.sort())
    print(shulist)


# shulist_0 = [1, 3, 44, 6, 7, 8, 98, 4, 44, 22, 333, 1254, 666]
# timu37(shulist_0)


def timu37_1(shulist):
    # 输入10个数
    numbers = shulist

    # 冒泡排序实现
    n = len(numbers)
    for i in range(n):
        for j in range(0, n-i-1):
            if numbers[j] > numbers[j+1]:
                numbers[j], numbers[j+1] = numbers[j+1], numbers[j]

    # print("冒泡排序结果:", numbers)


# shulist_1 = [1, 3, 44, 6, 7, 8, 98, 4, 44, 22, 333, 1254, 666]
# timu37_1(shulist_1)


"""
题目38：求一个3*3矩阵主对角线元素之和。
"""


def timu38():
    a = []
    sum = 0
    for i in range(0, 3):
        a.append([])
        print(a)
        for j in range(0, 3):
            linshi = float(input("请输入值："))
            a[i].append(linshi)
            print(a)

    for i in range(0, 3):
        sum += a[i][i]
    print(sum)


# timu38()

"""
题目39：有一个已经排好序的数组。现输入一个数，要求按原来的规律将它插入数组中。
"""


def timu39():
    pass


timu39()
